package com.guigu.guliorder.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.guliorder.entity.OmsOrderItem;
import com.guigu.guliorder.entity.dto.InsertOrderItemBatchDto;
import com.guigu.guliorder.entity.dto.SelectOrderItemPagesDto;
import com.guigu.guliorder.entity.dto.UpdateOrderItemDto;
import com.guigu.guliorder.mapper.OmsOrderItemMapper;
import com.guigu.guliorder.service.IOmsOrderItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.guigu.gulicommon.constant.enumes.res.AddResCode.INSERT_ORDER_ITEMS_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.DeleteResCode.DELETE_ORDER_ITEM_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.SELECT_ORDER_ITEM_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.UpdateResCode.UPDATE_ORDER_ITEM_SUCCESS;

/**
 * 订单项信息 服务实现类
 * </p>
 *
 * @author 陈俊杰
 * @since 2024-08-15
 */
@Service
public class OmsOrderItemServiceImpl extends ServiceImpl<OmsOrderItemMapper, OmsOrderItem> implements IOmsOrderItemService {
    /**
     * 分页查询订单项详情信息(通用)
     *
     * @param pagesDto 查询订单项的参数
     * @return vo 订单项详情的分页
     */
    @Override
    public Res<OmsOrderItem> findPages(SelectOrderItemPagesDto pagesDto) {
        LambdaQueryChainWrapper<OmsOrderItem> chainWrapper = new LambdaQueryChainWrapper<>(this.baseMapper);
        //1 封装分页条件
        Page<OmsOrderItem> pageQuery = new Page<>(pagesDto.getPageNum(), pagesDto.getPageSize());
        // 2 查询数据
        if (StringUtils.isBlank(pagesDto.getQueryParam())) {
            //2.1 查询参数为空,那么就进行不带参数查询
            Page<OmsOrderItem> omsOrderItemNotParamsList = chainWrapper.page(pageQuery);
            return new Res<>(SELECT_ORDER_ITEM_SUCCESS, omsOrderItemNotParamsList);
        }
        // 2.2 查询参数不为空,那么就进行带参数模糊查询
        Page<OmsOrderItem> omsOrderItemList = chainWrapper
                .like(OmsOrderItem::getOrderSn, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSkuName, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSpuName, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSpuBrand, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSkuPic, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSkuAttrsVals, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getOrderId, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSkuId, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSpuId, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getSpuPic, pagesDto.getQueryParam())
                .or()
                .like(OmsOrderItem::getCategoryId, pagesDto.getQueryParam())
                .page(pageQuery);
        return new Res<>(SELECT_ORDER_ITEM_SUCCESS, omsOrderItemList);
    }

    /**
     * 批量新增订单项(通用)
     *
     * @param dto 添加订单项的参数
     * @return vo 添加订单项的信息
     */
    @Override
    @Transactional
    public Res<OmsOrderItem> addBatch(ArrayList<InsertOrderItemBatchDto> dto) {
        //1.复制属性到OmsOrderItem实体类中
        List<OmsOrderItem> omsOrderItems = new ArrayList<>();
        BeanUtils.copyProperties(dto, omsOrderItems);
        //2. 批量新增OmsOrderItem
        this.saveBatch(omsOrderItems);
        return new Res<>(INSERT_ORDER_ITEMS_SUCCESS, omsOrderItems);
    }

    /**
     * 批量删除订单项(通用)
     *
     * @param ids 批量删除订单项的id集合
     * @return vo 被删除的订单项的信息
     */
    @Override
    @Transactional
    public Res<OmsOrderItem> deleteByIdsBatch(Long[] ids) {
        ArrayList<Long> idList = new ArrayList<>();
        //1. 删除数据
        this.removeBatchByIds(Arrays.asList(ids));
        //2. 封装被删除的信息
        List<OmsOrderItem> omsOrderItems = new ArrayList<>();
        idList.forEach(id -> {
            OmsOrderItem omsOrderItem = new OmsOrderItem().setId(id);
            omsOrderItems.add(omsOrderItem);
        });
        return new Res<>(DELETE_ORDER_ITEM_SUCCESS, omsOrderItems);
    }

    /**
     * 修改订单项(通用)
     *
     * @param dto 修改订单项的参数
     * @return vo 修改后订单项的信息
     */
    @Override
    public Res<OmsOrderItem> update(UpdateOrderItemDto dto) {
        //1.修改数据
        new LambdaUpdateChainWrapper<>(this.baseMapper)
                .set(StringUtils.isNotBlank(dto.getOrderSn()), OmsOrderItem::getOrderSn, dto.getOrderSn())
                .set(StringUtils.isNotBlank(dto.getSkuAttrsVals()), OmsOrderItem::getSkuAttrsVals, dto.getSkuAttrsVals())
                .set(StringUtils.isNotBlank(dto.getSkuName()), OmsOrderItem::getSkuName, dto.getSkuName())
                .set(StringUtils.isNotBlank(dto.getSkuPic()), OmsOrderItem::getSkuPic, dto.getSkuPic())
                .set(dto.getSkuPrice() != null, OmsOrderItem::getSkuPrice, dto.getSkuPrice())
                .set(dto.getSkuQuantity() != null, OmsOrderItem::getSkuQuantity, dto.getSkuQuantity())
                .set(StringUtils.isNotBlank(dto.getSpuBrand()), OmsOrderItem::getSpuBrand, dto.getSpuBrand())
                .set(StringUtils.isNotBlank(dto.getSpuName()), OmsOrderItem::getSpuName, dto.getSpuName())
                .set(StringUtils.isNotBlank(dto.getSpuPic()), OmsOrderItem::getSpuPic, dto.getSpuPic())
                .set(dto.getSpuId() != null, OmsOrderItem::getSpuId, dto.getSpuId())
                .set(dto.getSkuId() != null, OmsOrderItem::getSkuId, dto.getSkuId())
                .set(dto.getCategoryId() != null, OmsOrderItem::getCategoryId, dto.getCategoryId())
                .set(dto.getOrderId() != null, OmsOrderItem::getOrderId, dto.getOrderId())
                .set(dto.getRealAmount() != null, OmsOrderItem::getRealAmount, dto.getRealAmount())
                .set(dto.getPromotionAmount() != null, OmsOrderItem::getPromotionAmount, dto.getPromotionAmount())
                .set(dto.getCouponAmount() != null, OmsOrderItem::getCouponAmount, dto.getCouponAmount())
                .set(dto.getIntegrationAmount() != null, OmsOrderItem::getIntegrationAmount, dto.getIntegrationAmount())
                .set(dto.getGiftGrowth() != null, OmsOrderItem::getGiftGrowth, dto.getGiftGrowth())
                .set(dto.getGiftIntegration() != null, OmsOrderItem::getGiftIntegration, dto.getGiftIntegration())
                .eq(dto.getId() != null, OmsOrderItem::getId, dto.getId())
                .update();
        //2.封装修改后的信息
        OmsOrderItem omsOrderItem = this.baseMapper.selectById(dto.getId());
        return new Res<>(UPDATE_ORDER_ITEM_SUCCESS, omsOrderItem);
    }
}
